package cloud.xlh;

import cloud.xlh.util.RsaUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/1/27 19:00
 * @ Description:
 */
@SpringBootApplication
public class EncodeStarterApplication {
    private static String filePath="D:\\development\\environment\\idea\\IdeaProjects\\xlh_osms\\module\\encode-starter\\src\\main\\resources\\key.properties";
    //服务器启动：java -jar encode-starter-0.0.1-SNAPSHOT.jar /home/cmbwlt/key.properties
    public static void main(String[] args) throws Exception {
        //filePath = args[0];
        SpringApplication.run(EncodeStarterApplication.class , args);
        doService();
    }

    public static void doService() throws Exception {
        File file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
           /* Resource resource = new ClassPathResource("/application.yml");
            Properties properties = null;
            YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
            yamlPropertiesFactoryBean.setResources((resource));
            properties  = yamlPropertiesFactoryBean.getObject();
            String propertiesFilePath = (String) properties.get("propertiesFilePath");*/

        //首次使用，初始化密钥写入key.properties文件
        Properties properties = new Properties();
        //InputStream resourceAsStream = EncodeStarterApplication.class.getResourceAsStream("D:\\development\\environment\\idea\\IdeaProjects\\example\\encode-starter\\src\\main\\resources\\key.properties");
        FileInputStream fileInputStream = new FileInputStream(filePath);
        properties.load(fileInputStream);
        String rsaPrivateKey = properties.getProperty("rsaPrivateKey");
        String rsaPublicKey = properties.getProperty("rsaPublicKey");
        if (StringUtils.isEmpty(rsaPrivateKey) || StringUtils.isEmpty(rsaPublicKey)) {
            System.out.println("💕💕💕💕💕欢迎使用RSA非对称加密工具💕💕💕💕💕");
            System.out.println("💨💨💨💨首次使用，已为您生成密钥对，之后延用此密钥对，敬请享用！！！👍👍👍");
            //生成密钥并打印出来
            Map rsaMap = RsaUtil.genKeyPair();
            String rsaPrivateKeyForNewGen =  (String)RsaUtil.getPrivateKey(rsaMap);
            System.out.println("base64编码的私钥：" +  rsaPrivateKeyForNewGen);
            rsaPrivateKey = rsaPrivateKeyForNewGen;
            String rsaPublicKeyForNewGen = (String) RsaUtil.getPublicKey(rsaMap);
            rsaPublicKey = rsaPublicKeyForNewGen;
            System.out.println("base64编码的公钥：" + rsaPublicKeyForNewGen);
            //复制到key.properties文件中
            properties.put("rsaPrivateKey" , rsaPrivateKeyForNewGen);
            properties.put("rsaPublicKey" , rsaPublicKeyForNewGen);
            //String file = EncodeStarterApplication.class.getClassLoader().getResource("key.properties").getFile();
            BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(filePath));
            properties.store(out, "XuLeHuang , you are the best!!!");
        }

        while (true) {
            System.out.println("😉😉😉😉😉🌭操作类型");
            System.out.println("1.明文加密：");
            System.out.println("2.退出系统：");
            Scanner scanner = new Scanner(System.in);
            System.out.println("😘😘😘😘😘😘请输入操作类型😘😘😘😘😘😘");
            String opTy = scanner.nextLine();
            switch (opTy) {
                case "1" : doEncode(rsaPublicKey , rsaPrivateKey);break;
                case "2" : ;
                    System.out.println("感谢您的使用🦴🦴为您服务荣幸之至🙈🙈");
                    System.out.println("Copyright 2022-∞ XuLeHuang All rights reserved.");
                    System.exit(0);break;
                default: break;
            }
        }
    }

    public static void doEncode(String rsaPublicKey , String rsaPrivateKey) throws Exception {
        System.out.println("请输入明文密码!!!");
        Scanner scanner = new Scanner(System.in);
        String originalPassword = scanner.nextLine();
        System.out.println("原文密码：" + originalPassword);
        //加密
        String encryptPassword = RsaUtil.encrypt(originalPassword, rsaPublicKey);
        //输出
        System.out.println("加密后密码👌👌👌：\n" + encryptPassword);
        String decryptPassword = RsaUtil.decrypt(encryptPassword, rsaPrivateKey);
        System.out.println("解密后密码：\n" + decryptPassword);
    }
}
